<template>
    <el-container class="hmoe-container">
      <!-- 头部 -->
      <el-header>
        <div>
          <img src="../assets/heima.png" alt="">
          <span>电商后台管理系统</span>
        </div>
        <el-button type="info" @click="logOut">退出</el-button>
      </el-header>
      <!-- 主体 -->
      <el-container>
        <!-- 侧边栏 -->
        <el-aside :width="isCollapse ? '64px' : '200px'">
          <!-- 折叠按钮 -->
          <div class="toggle-btn" @click="toggleCollapse">|||</div>
          <!-- 侧边栏菜单 -->
          <!-- 通过router属性开启router模式,开启后会将index的值作为路径,在点击后传入 -->
          <el-menu
            unique-opened
            router
            :collapse=isCollapse
            :collapse-transition = "false"
            background-color="#333744"
            text-color="#fff"
            active-text-color="#2A8FFE"
            :default-active="activePath">
            <!-- 一级菜单 -->
            <!-- 菜单的index相同时,你点击一个菜单,所有的菜刀都会打开 -->
            <el-submenu  :index="'/' + item.path + ''" v-for="item in menulist" :key="item.id">
              <!-- 一级菜单的模板区 -->
              <template slot="title">
                <!-- 图标 -->
                <i :class="iconObj[item.id]">
                  <!-- <span :class="iconfont icon-user1"></span> -->
                  <!-- {{iconObj[item.id]}} -->
                </i>
                <!-- 文本 -->
                <span>{{item.authName}}</span>
              </template>
              <!-- 二级菜单 -->
              <el-menu-item @click="saveNavState(subitem.path)" :index="'/' + subitem.path + ''" v-for="subitem in item.children" :key="subitem.id">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{subitem.authName}}</span>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <!-- 右侧内容主体 -->
        <el-main>
          <!-- 路由占位符 -->
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
</template>

<script>
export default {
  created(){
    this.getMenuList();
    // 在创建dom时就将activePath赋值,相当于初始化
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  data(){
    return {
      // 左侧菜单数据
      menulist:[],
      iconObj:{
        '125':'iconfont icon-user1',
        '103':'iconfont icon-lifangtilitiduomiantifangkuai',
        '101':'iconfont icon-shangpin',
        '102':'iconfont icon-dingdan',
        '145':'iconfont icon-baobiao'
      },
      // 是否折叠
      isCollapse:false,
      activePath:''
    }
  },
  methods:{
    logOut(){
      // 退出登录只需要将session清空就可以了
      sessionStorage.clear();
      // 清空session后跳转到login页面
      this.$router.push('/login')
    },
    // 获取所有菜单数据
    async getMenuList() {
      const {data:res} = await this.$http.get('menus');
      if(res.meta.status !== 200) {
        return this.$message.error(res.data.msg);
      }
      console.log(res.data)
      // return this.$message.success(res.data.msg)
      this.menulist = res.data;
    },
    // 点击按钮切换侧边栏大小
    toggleCollapse(){
      this.isCollapse = !this.isCollapse
    },
    // 保存当前页面的path,保存链接的激活状态
    saveNavState(activePath){
      sessionStorage.setItem('activePath','/' + activePath);
      // 每次点击都为activePath赋值
      this.activePath = activePath;
    }
  }
}
</script>
<!--scoped 表示样式生效的区间，加上scoped就代表当前样式只在当前页面生效，反之则会全局生效-->
<style lang="less" scoped>
.hmoe-container {
  height: 100%;
}
.el-header {
  background-color: #363D40;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  font-size: 20px;
  color: #fff;

  div {
    display: flex;
    align-items: center;
    
    span {
      margin-left: 15px;
    }
  }
}

.el-menu {
  border: 0;
}

.el-aside {
  background-color: #313743;
}

.el-main {
  background-color: #E9EDF1;
}



.iconfont {
  margin-right: 10px;
}

.toggle-btn {
  background-color: #485164;
  text-align: center;
  color: #fff;
  font-size: 10px;
  line-height: 24px;
  // 设置文本间距
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>